/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.soap.SoapTransport;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.DistributionList;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.accesscontrol.GranteeType;
/*     */ import com.zimbra.cs.account.accesscontrol.TargetType;
/*     */ import com.zimbra.qa.unittest.prov.soap.SoapTest;
/*     */ import com.zimbra.soap.admin.message.CountObjectsRequest;
/*     */ import com.zimbra.soap.admin.message.CountObjectsResponse;
/*     */ import com.zimbra.soap.admin.type.CountObjectsType;
/*     */ import com.zimbra.soap.admin.type.DomainSelector;
/*     */ import com.zimbra.soap.admin.type.DomainSelector.DomainBy;
/*     */ import com.zimbra.soap.admin.type.GranteeSelector.GranteeBy;
/*     */ import com.zimbra.soap.type.TargetBy;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import junit.framework.TestCase;
/*     */ import org.junit.After;
/*     */ import org.junit.Before;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestCountObjects
/*     */   extends TestCase
/*     */ {
/*  50 */   private final Provisioning mProv = Provisioning.getInstance();
/*  51 */   private static String PASSWORD = "test123";
/*  52 */   private static String DOMAIN_ADMIN_USER = "domaintestadmin";
/*  53 */   private static String ROGUE_ADMIN_USER = "roguetestadmin";
/*  54 */   private final String DOMAIN_NAME = "testcountobjects.com";
/*     */   private String DOMAIN_ADMIN_USER_EMAIL;
/*     */   private String ROGUE_ADMIN_USER_EMAIL;
/*  57 */   private final ArrayList<String> testAccountIDs = new ArrayList();
/*  58 */   private final ArrayList<String> testDLIDs = new ArrayList();
/*  59 */   private final ArrayList<String> testDomainIDs = new ArrayList();
/*  60 */   private final ArrayList<String> testCOSIDs = new ArrayList();
/*     */   
/*     */ 
/*     */   @Before
/*     */   public void setUp()
/*     */     throws Exception
/*     */   {
/*  67 */     Map<String, Object> attrs = new HashMap();
/*  68 */     Domain domain = this.mProv.createDomain("testcountobjects.com", attrs);
/*  69 */     assertNotNull(domain);
/*  70 */     this.testDomainIDs.add(domain.getId());
/*     */     
/*     */ 
/*  73 */     for (int i = 0; i < 10; i++) {
/*  74 */       Map<String, Object> acctAttrs = new HashMap();
/*  75 */       Account acct = this.mProv.createAccount("count-test-acc" + i + "@" + "testcountobjects.com", PASSWORD, acctAttrs);
/*     */       
/*  77 */       assertNotNull(acct);
/*  78 */       this.testAccountIDs.add(acct.getId());
/*  79 */       this.mProv.addAlias(acct, "count-test-alias" + i + "@" + "testcountobjects.com");
/*     */     }
/*     */     
/*     */ 
/*  83 */     for (int i = 0; i < 10; i++) {
/*  84 */       Map<String, Object> acctAttrs = new HashMap();
/*  85 */       DistributionList dl = this.mProv.createDistributionList("count-test-DL" + i + "@" + "testcountobjects.com", acctAttrs);
/*     */       
/*  87 */       assertNotNull(dl);
/*  88 */       this.testDLIDs.add(dl.getId());
/*     */     }
/*     */     
/*     */ 
/*  92 */     for (int i = 0; i < 10; i++) {
/*  93 */       Map<String, Object> cosAttrs = new HashMap();
/*  94 */       Cos cos = this.mProv.createCos("count-test-cos" + i + "-" + "testcountobjects.com", cosAttrs);
/*     */       
/*  96 */       assertNotNull(cos);
/*  97 */       this.testCOSIDs.add(cos.getId());
/*     */     }
/*     */     
/*     */ 
/* 101 */     this.DOMAIN_ADMIN_USER_EMAIL = (DOMAIN_ADMIN_USER + "@" + "testcountobjects.com");
/* 102 */     attrs = new HashMap();
/* 103 */     attrs.put("zimbraIsDelegatedAdminAccount", "TRUE");
/*     */     
/* 105 */     Account acct = this.mProv.createAccount(this.DOMAIN_ADMIN_USER_EMAIL, PASSWORD, attrs);
/*     */     
/* 107 */     assertNotNull(acct);
/* 108 */     this.mProv.grantRight(TargetType.domain.getCode(), TargetBy.name, "testcountobjects.com", GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.name, this.DOMAIN_ADMIN_USER_EMAIL, null, "domainAdminRights", null);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 113 */     this.testAccountIDs.add(acct.getId());
/*     */     
/*     */ 
/*     */ 
/* 117 */     this.ROGUE_ADMIN_USER_EMAIL = (ROGUE_ADMIN_USER + "@" + "testcountobjects.com");
/* 118 */     attrs = new HashMap();
/* 119 */     attrs.put("zimbraIsDelegatedAdminAccount", "TRUE");
/*     */     
/* 121 */     acct = this.mProv.createAccount(this.ROGUE_ADMIN_USER_EMAIL, PASSWORD, attrs);
/* 122 */     assertNotNull(acct);
/* 123 */     this.mProv.grantRight(TargetType.domain.getCode(), TargetBy.name, "testcountobjects.com", GranteeType.GT_USER.getCode(), GranteeSelector.GranteeBy.name, this.ROGUE_ADMIN_USER_EMAIL, null, "countDistributionList", null);
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 128 */     this.testAccountIDs.add(acct.getId());
/*     */   }
/*     */   
/*     */   @After
/*     */   public void tearDown() throws Exception
/*     */   {
/* 134 */     for (String ID : this.testAccountIDs) {
/* 135 */       this.mProv.deleteAccount(ID);
/*     */     }
/* 137 */     for (String ID : this.testDLIDs) {
/* 138 */       this.mProv.deleteDistributionList(ID);
/*     */     }
/* 140 */     for (String ID : this.testCOSIDs) {
/* 141 */       this.mProv.deleteCos(ID);
/*     */     }
/* 143 */     for (String ID : this.testDomainIDs) {
/* 144 */       this.mProv.deleteDomain(ID);
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testDomainAdmin() throws Exception {
/*     */     try {
/* 151 */       SoapTransport transport = SoapTest.authAdmin(this.DOMAIN_ADMIN_USER_EMAIL, PASSWORD);
/*     */       
/*     */       try
/*     */       {
/* 155 */         CountObjectsRequest req = new CountObjectsRequest(CountObjectsType.domain);
/*     */         
/* 157 */         CountObjectsResponse resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 158 */         fail("should not be able to count domains");
/*     */       } catch (SoapFaultException e) {
/* 160 */         assertEquals("should not be able to count domains", "service.PERM_DENIED", e.getCode());
/*     */       }
/*     */       
/*     */ 
/*     */       try
/*     */       {
/* 166 */         CountObjectsRequest req = new CountObjectsRequest(CountObjectsType.cos);
/*     */         
/* 168 */         CountObjectsResponse resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 169 */         fail("should not be able to count COSes");
/*     */       } catch (SoapFaultException e) {
/* 171 */         assertEquals("should not be able to count COSes", "service.PERM_DENIED", e.getCode());
/*     */       }
/*     */       
/*     */ 
/*     */ 
/* 176 */       CountObjectsRequest req = new CountObjectsRequest(CountObjectsType.userAccount);
/*     */       
/* 178 */       CountObjectsResponse resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 179 */       assertTrue("should have at least one account", resp.getNum() > 0L);
/* 180 */       assertEquals("object type in response should be 'userAccount'", "userAccount", resp.getType());
/*     */       
/*     */ 
/* 183 */       req = new CountObjectsRequest(CountObjectsType.account);
/* 184 */       resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 185 */       assertTrue("should have at least one account", resp.getNum() > 0L);
/* 186 */       assertEquals("object type in response should be 'account'", "account", resp.getType());
/*     */       
/*     */ 
/* 189 */       req = new CountObjectsRequest(CountObjectsType.dl);
/* 190 */       resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 191 */       assertTrue("should have at least one distribution list", resp.getNum() > 0L);
/*     */       
/* 193 */       assertEquals("object type in response should be 'dl'", "dl", resp.getType());
/*     */       
/*     */ 
/* 196 */       req = new CountObjectsRequest(CountObjectsType.alias);
/* 197 */       resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 198 */       assertTrue("should have at least one alias", resp.getNum() > 0L);
/* 199 */       assertEquals("object type in response should be 'alias'", "alias", resp.getType());
/*     */       
/*     */ 
/* 202 */       req = new CountObjectsRequest(CountObjectsType.userAccount);
/* 203 */       req.addDomain(new DomainSelector(DomainSelector.DomainBy.name, "testcountobjects.com"));
/* 204 */       resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 205 */       assertTrue("should have at least one userAccount", resp.getNum() > 0L);
/*     */       
/* 207 */       assertEquals("object type in response should be 'userAccount'", "userAccount", resp.getType());
/*     */       
/*     */ 
/* 210 */       req = new CountObjectsRequest(CountObjectsType.account, new DomainSelector(DomainSelector.DomainBy.name, "testcountobjects.com"));
/* 211 */       resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 212 */       assertTrue("should have at least one account", resp.getNum() > 0L);
/* 213 */       assertEquals("object type in response should be 'account'", "account", resp.getType());
/*     */       
/*     */ 
/* 216 */       req = new CountObjectsRequest(CountObjectsType.dl, new DomainSelector(DomainSelector.DomainBy.name, "testcountobjects.com"));
/* 217 */       resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 218 */       assertTrue("should have at least one distribution list", resp.getNum() > 0L);
/*     */       
/* 220 */       assertEquals("object type in response should be 'dl'", "dl", resp.getType());
/*     */       
/*     */ 
/* 223 */       req = new CountObjectsRequest(CountObjectsType.alias, new DomainSelector(DomainSelector.DomainBy.name, "testcountobjects.com"));
/* 224 */       resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 225 */       assertTrue("should have at least one alias", resp.getNum() > 0L);
/* 226 */       assertEquals("object type in response should be 'alias'", "alias", resp.getType());
/*     */     }
/*     */     catch (Exception e) {
/* 229 */       fail(e.getLocalizedMessage());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testRogueAdmin() throws Exception {
/*     */     try {
/* 236 */       SoapTransport transport = SoapTest.authAdmin(this.ROGUE_ADMIN_USER_EMAIL, PASSWORD);
/*     */       
/*     */       try
/*     */       {
/* 240 */         CountObjectsRequest req = new CountObjectsRequest(CountObjectsType.domain);
/* 241 */         CountObjectsResponse resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 242 */         fail("should not be able to count domains");
/*     */       } catch (SoapFaultException e) {
/* 244 */         assertEquals("service.PERM_DENIED", e.getCode());
/*     */       }
/*     */       
/*     */       try
/*     */       {
/* 249 */         CountObjectsRequest req = new CountObjectsRequest(CountObjectsType.userAccount, new DomainSelector(DomainSelector.DomainBy.name, "testcountobjects.com"));
/*     */         
/* 251 */         CountObjectsResponse resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 252 */         fail("should not be able to count accounts");
/*     */       } catch (SoapFaultException e) {
/* 254 */         assertEquals("service.PERM_DENIED", e.getCode());
/*     */       }
/*     */       
/*     */       try
/*     */       {
/* 259 */         CountObjectsRequest req = new CountObjectsRequest(CountObjectsType.account, new DomainSelector(DomainSelector.DomainBy.name, "testcountobjects.com"));
/*     */         
/* 261 */         CountObjectsResponse resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 262 */         fail("should not be able to count accounts");
/*     */       } catch (SoapFaultException e) {
/* 264 */         assertEquals("service.PERM_DENIED", e.getCode());
/*     */       }
/*     */       
/*     */       try
/*     */       {
/* 269 */         CountObjectsRequest req = new CountObjectsRequest(CountObjectsType.dl);
/*     */         
/* 271 */         req.addDomain(new DomainSelector(DomainSelector.DomainBy.name, "testcountobjects.com"));
/* 272 */         CountObjectsResponse resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 273 */         assertTrue("should have at least one distribution list", resp.getNum() > 0L);
/*     */       }
/*     */       catch (ServiceException e) {
/* 276 */         fail("should not be throwing exception here " + e.getLocalizedMessage());
/*     */       }
/*     */       
/*     */ 
/*     */       try
/*     */       {
/* 282 */         CountObjectsRequest req = new CountObjectsRequest(CountObjectsType.alias, new DomainSelector(DomainSelector.DomainBy.name, "testcountobjects.com"));
/*     */         
/* 284 */         CountObjectsResponse resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 285 */         fail("should not be able to count aliases");
/*     */       } catch (SoapFaultException e) {
/* 287 */         assertEquals("service.PERM_DENIED", e.getCode());
/*     */       }
/*     */     } catch (Exception e) {
/* 290 */       fail(e.getLocalizedMessage());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testSuperAdmin() {
/*     */     try {
/* 297 */       SoapTransport transport = TestUtil.getAdminSoapTransport();
/*     */       
/* 299 */       CountObjectsRequest req = new CountObjectsRequest(CountObjectsType.domain);
/*     */       
/* 301 */       CountObjectsResponse resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 302 */       assertTrue("should have at least one domain", resp.getNum() > 0L);
/*     */       
/*     */ 
/* 305 */       req = new CountObjectsRequest(CountObjectsType.userAccount);
/* 306 */       resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 307 */       assertTrue("should have at least one userAccount", resp.getNum() > 0L);
/*     */       
/*     */ 
/*     */ 
/* 311 */       req = new CountObjectsRequest(CountObjectsType.account);
/* 312 */       resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 313 */       assertTrue("should have at least one account", resp.getNum() > 0L);
/*     */       
/*     */ 
/* 316 */       req = new CountObjectsRequest(CountObjectsType.dl);
/* 317 */       resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 318 */       assertTrue("should have at least one distribution list", resp.getNum() > 0L);
/*     */       
/*     */ 
/*     */ 
/* 322 */       req = new CountObjectsRequest(CountObjectsType.alias);
/* 323 */       resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 324 */       assertTrue("should have at least one alias", resp.getNum() > 0L);
/*     */       
/*     */ 
/* 327 */       req = new CountObjectsRequest(CountObjectsType.cos);
/* 328 */       resp = (CountObjectsResponse)SoapTest.invokeJaxb(transport, req);
/* 329 */       assertTrue("should have at least one COS", resp.getNum() > 0L);
/*     */     } catch (Exception e) {
/* 331 */       fail(e.getLocalizedMessage());
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestCountObjects.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */